<?php

namespace app\common\model;

use think\Model;
use think\Validate;

class AdminRole extends Model
{
    
    /**
     * 保存
     * @param array $data
     */
    public function saveData($data)
    {
        $validate = Validate::make([
            'title' => 'require'
        ], [
            'title' =>  '角色名称必须',
        ]);
        if (!$validate->check($data)) {
            exception($validate->getError());
        }
        if (!empty($data['id'])) {
            return $this->edit($data);
        } else {
            return $this->add($data);   
        }
    }

    public function add($data)
    {
        return $this->insert($data);
    }

    public function edit($data)
    {
        return $this->where('id', $data['id'])->update($data);
    }
    
    public function delData($id)
    {
        return $this->where([['id', '=', $id], ['is_super', '=', 0]])->delete();
    }

    public function getAuth($id)
    {
        $data = $this->where('id', $id)->find();
        $auth = explode(',', $data['auth']);
        return $auth;
    }
    /**
     * 权限修改
     * @return [type] [description]
     */
    public function saveAuth($data)
    {
        $validate = Validate::make([
            'id' => 'require',
            'auth' => 'require'
        ], [
            'id' =>  '角色名称必须',
            'auth' =>  '权限必须',
        ]);
        if (!$validate->check($data)) {
            exception($validate->getError());
        }
        $data['auth'] = implode(',', $data['auth']);
        return $this->edit($data);

    }
}